﻿using Microsoft.ProjectOxford.Face;
using Microsoft.ProjectOxford.Face.Contract;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media.Capture;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Core;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

//“空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 上有介绍

namespace CameraControlDemo
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        static string personGroupName="myFriends";

        public MainPage()
        {
            this.InitializeComponent();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private async void btnCapture_Click(object sender, RoutedEventArgs e)
        {
            //face-sdk-service
            var faceService = new FaceServiceClient(Common.Consts.appKey);
            
            //M1:CameraCaptureUI
            CameraCaptureUI capture = new CameraCaptureUI();
            capture.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
            capture.PhotoSettings.AllowCropping = false;
            //capture.PhotoSettings.CroppedSizeInPixels = new Size(600,600);
            try
            {
                StorageFile photo = await capture.CaptureFileAsync(CameraCaptureUIMode.Photo);
                if (photo == null)
                {
                    return;
                }
                BitmapImage bitmapImage = new BitmapImage();
                using (IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read))
                {
                    bitmapImage.SetSource(stream);
                    imgShow.Source = bitmapImage;
                }
                //SDK ==  
                //Stream 转换
                using (Stream stream = File.Open(photo.Path, FileMode.Open))
                {
                    Face[] faces = await faceService.DetectAsync(stream, analyzesAge:true,analyzesGender: true);
                    System.Diagnostics.Debug.WriteLine("检测到" + faces.Count() + "张脸");
                    Output("检测到" + faces.Count() + "张脸");
                    foreach (Face item in faces)
                    {
                        System.Diagnostics.Debug.WriteLine("FaceId:" + item.FaceId + ",年龄:" + item.Attributes.Age + ",性别:" + item.Attributes.Gender);
                        Output("FaceId:" + item.FaceId + ",年龄" + item.Attributes.Age + ",性别" + item.Attributes.Gender);
                    }
                }

                //API ==

            }
            catch (Exception ex)
            {
                throw;
            }
            //M2:

           
        }   

        public void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        private async void Output(string str)
        {
            //await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High,async ()=> { await new MessageDialog(str).ShowAsync(); });
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High,  () => { txtbOutput.Text += str; });
        }
    }
}
